/*
 * NewPhoneInterface.java
 *
 * Created on May 11, 2006, 10:43 AM
 *
 * @author  Francois Daniels
 */

import java.awt.event.*;
import java.awt.*;
import za.gateway.preferences.Settings;
import za.gateway.io.*;
import za.gateway.backend.*;
import za.gateway.container.*;

public class NewPhoneInterface extends javax.swing.JDialog 
        implements ActionListener {
    
    /** Creates new form NewPhoneInterface */
    public NewPhoneInterface (java.awt.Frame parent, boolean modal) {
        super (parent, modal);
        initComponents ();
        updateComponents ();
    }
    
    public NewPhoneInterface (javax.swing.JDialog parent, 
            boolean modal) {
        
        super (parent, modal);
        initComponents ();
        updateComponents ();
    }
    
    private void updateComponents () {
        addButton.addActionListener (this);
        cancelButton.addActionListener (this);
        if (Settings.isWindows) {
            try {
                javax.swing.UIManager.setLookAndFeel (
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                javax.swing.SwingUtilities.
                        updateComponentTreeUI (this);
                pack ();
            } catch (Exception e) { }
        }
        portText.setVisible (true);
        portLabel.setVisible (true);
        usernameLabel.setVisible (false);
        usernameText.setVisible (false);
        passwordLabel.setVisible (false);
        passwordText.setVisible (false);
    }
    
    public void setVisible (boolean b) {
        centerOnParentAndShow ();
        super.setVisible (b);
    }
    
    public void actionPerformed (ActionEvent e) {
        if (e.getSource () == addButton) {
            //TOOD: Add code to add data to the data
            //Actualy the data and return will be tested
            //Use will be notified of any errors that occured during 
            //the processing of the event
            
            dispose ();
        } else
            if (e.getSource () == cancelButton) {
            dispose ();
            }
    }
    
    public void showWindow (String name, String phone, 
            String description, String username, String password) {
        nameText.setText (name);
        phoneText.setText (phone);
        usernameText.setText (username);
        passwordText.setText (password);
        descriptionText.setText (description);
        setTitle ("Edit Phone Entry");
        setVisible (true);
    }
    
    private void centerOnParentAndShow () {
        pack ();
        Container parent = getParent ();
        if (parent == null) return;
        
        Dimension dparent = parent.getSize ();
        Dimension window = getSize ();
        int xCoord, yCoord;
        try {
            xCoord = parent.getLocationOnScreen ().x + 
                    (dparent.width/2 - window.width/2);
            yCoord = parent.getLocationOnScreen ().y + 
                    (dparent.height/2 - window.height/2);
        } catch (IllegalComponentStateException e) {return;}
        
        //Ensure that no part of the window will be off-screen
        Dimension screen = Toolkit.getDefaultToolkit ().
                getScreenSize ();
        int xOffScreenExcess = xCoord + window.width - screen.width;
        if ( xOffScreenExcess > 0 ) {
            xCoord = xCoord - xOffScreenExcess;
        }
        if (xCoord < 0 ) {
            xCoord = 0;
        }
        int yOffScreenExcess = yCoord + window.height - screen.height;
        if ( yOffScreenExcess > 0 ) {
            yCoord = yCoord - yOffScreenExcess;
        }
        if (yCoord < 0) {
            yCoord = 0;
        }
        
        setLocation ( xCoord, yCoord );
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Code generated by Netbeans for the GUI Components  ">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        javax.swing.JLabel jLabel1;
        javax.swing.JLabel jLabel2;
        javax.swing.JLabel jLabel3;
        javax.swing.JSeparator jSeparator1;

        type = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        nameText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        phoneText = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionText = new javax.swing.JTextArea();
        usernameLabel = new javax.swing.JLabel();
        usernameText = new javax.swing.JTextField();
        passwordText = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        regularPhone = new javax.swing.JRadioButton();
        softPhone = new javax.swing.JRadioButton();
        portLabel = new javax.swing.JLabel();
        portText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        locationText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Phone Entry");
        setResizable(false);
        jLabel1.setText("Name:");

        jLabel2.setText("Phone Number: ");

        jLabel3.setText("Description:");

        descriptionText.setColumns(20);
        descriptionText.setRows(5);
        jScrollPane1.setViewportView(descriptionText);

        usernameLabel.setText("Username:");

        passwordLabel.setText("Password:");

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Type of Phone"));
        type.add(regularPhone);
        regularPhone.setSelected(true);
        regularPhone.setText("Regular Phone");
        regularPhone.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        regularPhone.setMargin(new java.awt.Insets(0, 0, 0, 0));
        regularPhone.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                regularPhoneStateChanged(evt);
            }
        });

        type.add(softPhone);
        softPhone.setText("Software Phone");
        softPhone.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        softPhone.setMargin(new java.awt.Insets(0, 0, 0, 0));
        softPhone.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                softPhoneStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(regularPhone)
                    .add(softPhone))
                .addContainerGap(61, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(regularPhone)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(softPhone)
                .addContainerGap())
        );

        portLabel.setText("Port:");

        jLabel4.setText("Location");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(nameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 163, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel2)
                                    .add(jLabel4))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(phoneText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                                    .add(locationText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, portText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(portLabel)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                    .add(jLabel3)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(usernameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(usernameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 161, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(passwordLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 223, Short.MAX_VALUE)
                        .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(nameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(phoneText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(locationText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(portText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(portLabel))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 116, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(usernameLabel)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(usernameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(passwordLabel)
                        .add(passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addButton)
                    .add(cancelButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void cancelButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose ();
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    private void softPhoneStateChanged (javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_softPhoneStateChanged
        portText.setVisible (false);
        portLabel.setVisible (false);
        usernameLabel.setVisible (true);
        usernameText.setVisible (true);
        passwordLabel.setVisible (true);
        passwordText.setVisible (true);
    }//GEN-LAST:event_softPhoneStateChanged
    
    private void regularPhoneStateChanged (javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_regularPhoneStateChanged
        portText.setVisible (true);
        portLabel.setVisible (true);
        usernameLabel.setVisible (false);
        usernameText.setVisible (false);
        passwordLabel.setVisible (false);
        passwordText.setVisible (false);
    }//GEN-LAST:event_regularPhoneStateChanged
    
    private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        if (regularPhone.isSelected ()) {
            Phones phones = new Phones (Settings.userName, 
                    Settings.password, Settings.IP, Settings.database);
            String name = nameText.getText ();
            String phon = phoneText.getText ();
            String desc = descriptionText.getText ();
            String port = portText.getText ();
            String loca = locationText.getText ();
            
            phones.Add (name,phon,desc,loca,port);
            phones.Save ();
        } else {
            SoftPhones phones = new SoftPhones (Settings.userName,
                    Settings.password, Settings.IP, Settings.database);
            String name = nameText.getText ();
            String phon = phoneText.getText ();
            String user = usernameText.getText ();
            String pass = passwordText.getText ();
            String desc = descriptionText.getText ();
            String loca = locationText.getText ();
            
            phones.Add (name,phon,user,pass,desc,loca);
            phones.Save ();
        }
        dispose ();
    }//GEN-LAST:event_addButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main (String args[]) {
        java.awt.EventQueue.invokeLater (new Runnable () {
            public void run () {
                new NewPhoneInterface (new javax.swing.JFrame (),
                        true).setVisible (true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextArea descriptionText;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField locationText;
    private javax.swing.JTextField nameText;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField passwordText;
    private javax.swing.JTextField phoneText;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField portText;
    private javax.swing.JRadioButton regularPhone;
    private javax.swing.JRadioButton softPhone;
    private javax.swing.ButtonGroup type;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameText;
    // End of variables declaration//GEN-END:variables
    
}
